package com.ruoyi.web.pgci.controller;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.pgci.listener.PendingActivitiesListenner;
import com.ruoyi.web.pgci.listener.TrainingMatrixListener;
import com.ruoyi.web.pgci.service.ReadExcelService;
import com.ruoyi.web.pgci.vo.ReadPendingActivitiesVo;
import com.ruoyi.web.pgci.vo.ReadTrainingMatrixVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author joy
 * @create 2023-11-16
 * @description
 */
@Api("excel导入")
@RestController
@RequestMapping("importExcel")
public class PgciImportExcelController extends BaseController {

    @Autowired
    private ReadExcelService readExcelService;


    @ApiOperation("Training Matrix excel导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流", dataType = "File", dataTypeClass = File.class),
    })
    @PostMapping("/importTrainingMatrixExcel")
    public R importTrainingMatrixExcel(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            InputStream inputStream2 = file.getInputStream();
            //读取课程信息
            EasyExcel.read(inputStream, ReadTrainingMatrixVo.class, new TrainingMatrixListener(readExcelService)).headRowNumber(6).sheet().doRead();
            readExcelService.savePostCourseLearnReq(inputStream2);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流", dataType = "File", dataTypeClass = File.class),
    })
    @ApiOperation("Pending_activities.xlsx excel导入")
    @PostMapping("/importPendingActivities")
    public R importPendingActivities(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            //读取课程信息
            EasyExcel.read(inputStream, ReadPendingActivitiesVo.class, new PendingActivitiesListenner(readExcelService)).headRowNumber(10).sheet(1).doRead();
            InputStream inputStream2 = file.getInputStream();

            readExcelService.saveSailorsCourseLearnRecord(inputStream2);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
